/// 
/// Author: Jeff Liu
/// Date: 2021-08-11 13:30:11
/// LastEditTime: 2022-03-10 13:54:57
/// LastEditors: Jeff Liu
/// Description: 
/// FilePath: /HCC/Utils/SchemaValidate.cls
/// Jeff.liu@intersytems.com
/// 
/// xml schema验证方法
Class HCC.Utils.SchemaValidate Extends %RegisteredObject
{

/// 验证xml schema
ClassMethod Validate(Msg As %Stream.GlobalCharacter, SchemaFolder As %String, ByRef Errors As %String) As %Status
{
   
  set ErrMsg =""
 Set tSchema="urn:hl7-org:v3 "_SchemaFolder_"CDA.xsd"	
 
 Set tSC=##class(%XML.TextReader).ParseStream(Msg,.tReader,,,,tSchema)
 
 // set tSC=##class(%XML.TextReader).ParseString(Msg,.tReader,,,,tSchema)
 set verifyResult=0	
 set errCount=0
 If $$$ISERR(tSC) 
 {
	 set Errors= $System.Status.GetErrorText(tSC)
	 Quit tSC
 }
 		
 While (tReader.Read()) 	
  {			 
	if (tReader.NodeType="error") 	
		
	{			
			 set errCount= errCount+1
		set ErrMsg= ErrMsg_"*ERROR* "_tReader.Value_$char(10)_tReader.Path_$char(10)		
	}	
  }	
  if (errCount=0)
  {
  return $$$OK
  }
  set Errors=ErrMsg
  return $$$ERROR("General Error")
}

ClassMethod GetDocType(pXML As %Stream.GlobalCharacter) As %String
{
   	set tRoot=""
 	Set tStatus = ##class(%XML.XPATH.Document).CreateFromStream(pXML, .tDocument)
	Set tStatus = tDocument.EvaluateExpression("/*", "name()", .tResults)
	
	Set tStatus = pXML.Rewind()
	If (tResults.Count() > 0) Set tRoot = tResults.GetAt(1).Value
	Else  Set tRoot = "<errorNoRootElement>"
	
	quit tRoot
}

}
